package com.baroxtech.anoterecorder;

import android.bluetooth.BluetoothDevice;
import android.media.midi.MidiDevice;
import android.media.midi.MidiDeviceInfo;
import android.os.Bundle;

import androidx.annotation.NonNull;

// 用于描述一个 MIDI 设备的结构
public class AMIDIDev {
    BluetoothDevice bleDev;
    MidiDeviceInfo devInfo;
    MidiDevice device; // 在 open 成功后会被 device manager 来设置

    public AMIDIDev(@NonNull BluetoothDevice bleDev) {
        this.bleDev = bleDev;
    }

    public AMIDIDev(@NonNull MidiDeviceInfo info) {
        this.devInfo = info;
    }

    public boolean isUsb() {
        return devInfo != null && devInfo.getType() == MidiDeviceInfo.TYPE_USB;
    }

    public String getName() {
        if (bleDev != null) {
            return bleDev.getName();
        }
        return devInfo.getProperties().getString(MidiDeviceInfo.PROPERTY_NAME);
    }

    private static String getBLEDevKey(BluetoothDevice dev) {
        return dev.getName() + dev.getAddress();
    }

    BluetoothDevice getBleDev() {
        if (this.bleDev != null) return this.bleDev;
        Bundle b = devInfo.getProperties();
        return (BluetoothDevice) b.get(MidiDeviceInfo.PROPERTY_BLUETOOTH_DEVICE);
    }

    boolean isEqual(final @NonNull AMIDIDev dev) {
        boolean devType = isUsb();
        // 类型不同
        if (devType != dev.isUsb()) return false;
        // 判定 id
        if (devInfo != null && dev.devInfo != null) {
            return devInfo.getId() == dev.devInfo.getId();
        } else {
            return getBleDev().equals(dev.getBleDev());
        }
    }
}
